<?php
/**
 * * cpager-- for page class
 */
class cpager{
	const DEFAULT_REC_PER_PAGE=20;
	public $recTotal;
	public $recPerPage;
	public $pageTotal;
	public $pageID;
	private $app;
	private $moduleName;
	private $methodName;
	private $params;
	private $lang;
	
	public function __construct($recTotal=0, $recPerPage=20, $pageID=1){
		
		$this->setRecTotal($recTotal);
		$this->setRecPerPage($recPerPage);
		$this->setPageTotal();
		$this->setPageId($pageID);
		
		$this->setLang();
		$this->setApp();
		$this->setModule();
		$this->setMethod();
	}
	
	private  function setApp(){
		global $app;
		$this->app = $app;
	}
	private function setModule(){
		$this->moduleName=$this->app->getModuleName();
	}
	private function setMethod(){
		$this->methodName = $this->app->getMethodName();
	}
	public function setRecTotal($recT=0){
		$this->recTotal=$recT;
	}
	public function setRecPerPage($recper=20){
		$this->recPerPage=$recper;
	}
	public function setPageTotal(){
		$this->pageTotal=ceil($this->recTotal/$this->recPerPage);
	}
	public function setPageId($pageid=1){
		$pageid=(int) $pageid;
		if($pageid<0 || $pageid>$this->pageTotal) $pageid=1;
		$this->pageID = $pageid;
	}
	public function setLang(){
		global $lang;
		$this->lang=$lang;
	}
	
	private function setParams(){
		$this->params = $this->app->getParams();
		foreach ($this->params as $key=>$val){
			if(strtolower($key) == 'rectotal') $this->params[$key]=$this->recTotal;
			if(strtolower($key) == 'recperpage') $this->params[$key]=$this->recPerPage;
			if(strtolower($key) == 'pageid') $this->params[$key] = $this->pageID;
		}
	}
	
	public function limit(){
		$str='';
		if($this->pageID>=1) $str=' limit '.($this->pageID-1)*$this->recPerPage.' , '. $this->recPerPage;
		return $str;
		//die($str);
	}
	
	///////////////////////////初始化完毕
	
	public function createPrePage(){ // pre page
		$prestr='';
		if($this->pageID>1){
			$this->params['pageid']=$this->pageID-1;
			$prestr="<a class=\"next\" href=\"" .helper::createLink($this->moduleName, $this->methodName, $this->params). "\">&lt;" ;
			$prestr.=$this->lang->cpager->pre;
			$prestr.="</a>";
		}
		return $prestr;			
	}
	public function createNextPage(){
		$str='';
		if($this->pageID<$this->pageTotal){
			$this->params['pageid']=$this->pageID+1;
			$str="<a class=\"next\" href=\"" .helper::createLink($this->moduleName, $this->methodName, $this->params). "\">";
			$str.=$this->lang->cpager->next;
			$str.=" &gt;</a>";
		}
		return $str;
	}
	public function createNumberLinks(){
		$str='';
		//if($this->pageID)
		
		//just show 10 pages at most
		$beginPage=$this->pageID-5; $endPage=$this->pageID+5; // default
		if($beginPage<0){
			$beginPage=1;
			if($endPage+6<=$this->pageTotal){
				$endPage=10;
			}
		}
		if($endPage>$this->pageTotal){
			$endPage=$this->pageTotal;
		}
		for($i=$beginPage; $i<=$endPage; $i++){
			if($i!=$this->pageID){
				$this->params['pageid']=$i;
				$str.="<a class=\"flag_pg\" href=\"". helper::createLink($this->moduleName, $this->methodName, $this->params). "\">";
				$str.=$i;
				$str.="</a>";
			}else{
				$str.="<span class=\"current\">" . $i . "</span>";
			}			
		}
		return $str;
	}
	
	public function get(){
		if($this->pageID==0) return '<div id="pager">'.$this->lang->norec.'</div>';
		
		$this->setParams();
		
		$str='';
		$str.=$this->createPrePage();
		$str.=$this->createNumberLinks();
		$str.=$this->createNextPage();
		//$str.='-----'.$this->pageTotal;
		return '<div id="pager"><div class="pg">'.$str.'</div></div>';
	}
	
}